

package com.hazelcast.internal.serialization.impl.bufferpool;

import com.hazelcast.internal.nio.BufferObjectDataInput;
import com.hazelcast.internal.nio.BufferObjectDataOutput;
import com.hazelcast.internal.serialization.Data;

/**
 * The BufferPool allows the pooling of the {@link BufferObjectDataInput} and {@link BufferObjectDataOutput} instances.
 * <p>
 * The BufferPool is accessed using the {@link BufferPoolThreadLocal} So each thread gets its own instance and therefore
 * it doesn't need to be thread-safe.
 */
public interface BufferPool {

    /**
     * Takes an BufferObjectDataOutput from the pool.
     *
     * @return the taken BufferObjectDataOutput.
     */
    BufferObjectDataOutput takeOutputBuffer();

    /**
     * Returns a BufferObjectDataOutput back to the pool.
     * <p>
     * The implementation is free to not return the instance to the pool but just close it.
     *
     * @param out the BufferObjectDataOutput.
     */
    void returnOutputBuffer(BufferObjectDataOutput out);

    /**
     * Takes an BufferObjectDataInput from the pool and initializes it with the given data.
     * <p>
     * The reason that Data is passed as argument, is that for HazelcastEnterprise different
     * BufferObjectDataInput can be returned based on the type of Data.
     *
     * @return the taken BufferObjectDataInput
     */
    BufferObjectDataInput takeInputBuffer(Data data);

    /**
     * Returns a BufferObjectDataInput back to the pool.
     * <p>
     * The implementation is free to not return the instance to the pool.
     * In that case this call does not keep a reference to `BufferObjectDataInput` so that it can be
     * garbage-collected.
     *
     * @param in the BufferObjectDataInput.
     */
    void returnInputBuffer(BufferObjectDataInput in);
}
